<?php 
class ControllerCommonPaymentOnline extends Controller {
	private $error = array();
	public function login()
	{
		if ($this->customer->isLogged()) {
	  		$this->redirect($this->url->link('account/account', '', 'SSL'));
    	}
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validatelogin()) {
			unset($this->session->data['guest']);
			//$this->model_account_customer->addCustomer($this->request->post);
			$this->redirect($this->url->link('common/payment_online_logged', '', 'SSL'));
    	
	  	  	//$this->redirect($this->url->link('common/register/success'));
    	} 
	}
	private function validatelogin() {
		if ((utf8_strlen($this->request->post['username_p1']) < 1) || (utf8_strlen($this->request->post['username_p1']) > 32)) {
      		$this->error['error_username_p1'] = $this->language->get('error_username');
    	}
    	if ((utf8_strlen($this->request->post['password_p1']) < 1) || (utf8_strlen($this->request->post['password_p1']) > 32)) {
      		$this->error['error_password_p1'] = $this->language->get('error_password');
    	}
		if (!$this->error) {
			$this->data['error_login'] = false;
      		return true;
    	} else {
			$this->data['error_login'] = true;
      		return false;
    	}
	}
  	public function index() {
		$this->session->data['payment_invoice_type'] = 1; //'SMB-DHOL-'.date("dmy");
		
		if ($this->customer->isLogged()) {
	  		$this->redirect($this->url->link('common/payment_online_logged', '', 'SSL'));
    	}

    	$this->language->load('account/register');
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		$this->load->model('catalog/information');
		$this->data['error_info'] = false;
		$this->data['error_login'] = false;
		$this->data['error_shipping'] = false;
		$this->data['common_confirm'] = false;
		$info_end = $this->model_catalog_information->getInformation(55);
		$this->data['info_end'] = html_entity_decode($info_end['description'], ENT_QUOTES, 'UTF-8');
		
		$info_payment_online = $this->model_catalog_information->getInformation(59);
		$this->data['info_payment_online'] = html_entity_decode($info_payment_online['description'], ENT_QUOTES, 'UTF-8');
		
		$this->data['isshipping'] = false;
		
		if(isset($this->request->post['payment_online_login']))
		{
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validatelogin()) {
				unset($this->session->data['guest']);
				//$this->model_account_customer->addCustomer($this->request->post);
				//$this->customer->login($this->request->post['username_p1'], $this->request->post['password_p1']);
				//$this->redirect($this->url->link('common/register/success'));
				if (!$this->customer->login($this->request->post['username_p1'], $this->request->post['password_p1'])) {
      				$this->error['error_warning_login'] = $this->language->get('error_login');
					//$this->data['error_login'] =true;
				}
				else {
					$this->redirect($this->url->link('common/payment_online', '', 'SSL')); 
				}
			} 
		}
		if(isset($this->request->post['payment_online_register']))
		{
			if(($this->request->server['REQUEST_METHOD'] == 'POST') && $this->request->post['isshipping']==1) 		{
				$this->data['isshipping'] = true;
			}
			//$t1 = $this->validate();
			//$t2 = $this->validateshipping();
			//print_r($t1);
			//print_r($t2);
			//print_r($this);
			
			if($this->request->post['isshipping']==0)
			{
					//$c1 = $this->validate();
				  // echo $c1.'---';
					//echo '---';
					//print_r($this->error);
					if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
						$this->initSesstionGuestCheckout();
						$this->initSesstionPaymentShipping();
						$this->data['common_confirm'] = true;
						if(isset($this->request->post['newsletter'])) {
							$this->load->model('account/customer');
							$this->model_account_customer->joinNewsletter($this->request->post['email']);
						}
						//$this->data['error_info'] = true;
						//$this->data['error_shipping'] = true;
					}  else {
						$this->data['common_confirm'] = false;
						
						//$this->data['error_info'] = false;
						//$this->data['error_shipping'] = false;
					}
			}
			else {
					
					if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() && $this->validateshipping()) {
						$this->initSesstionGuestCheckout();
						$this->initSesstionPaymentShipping();
						$this->data['common_confirm'] = true;
						//$this->data['error_info'] = true;
						//$this->data['error_shipping'] = true;
						
						if(isset($this->request->post['newsletter'])) {
							$this->load->model('account/customer');
							$this->model_account_customer->joinNewsletter($this->request->post['email']);
						}
						
					}  else {
						$this->data['common_confirm'] = false;
						//$this->data['error_info'] = false;
						//$this->data['error_shipping'] = false;
					}
				
			}
			
		}

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_register'),
			'href'      => $this->url->link('common/register', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select'] = $this->language->get('text_select');
    	$this->data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', '', 'SSL'));
    	$this->data['text_your_details'] = $this->language->get('text_your_details');
    	$this->data['text_your_address'] = $this->language->get('text_your_address');
    	$this->data['text_your_password'] = $this->language->get('text_your_password');
		$this->data['text_newsletter'] = $this->language->get('text_newsletter');
				
    	$this->data['entry_firstname'] = $this->language->get('entry_firstname');
    	$this->data['entry_lastname'] = $this->language->get('entry_lastname');
    	$this->data['entry_email'] = $this->language->get('entry_email');
    	$this->data['entry_telephone'] = $this->language->get('entry_telephone');
    	$this->data['entry_fax'] = $this->language->get('entry_fax');
    	$this->data['entry_company'] = $this->language->get('entry_company');
    	$this->data['entry_address_1'] = $this->language->get('entry_address_1');
    	$this->data['entry_address_2'] = $this->language->get('entry_address_2');
    	$this->data['entry_postcode'] = $this->language->get('entry_postcode');
    	$this->data['entry_city'] = $this->language->get('entry_city');
    	$this->data['entry_country'] = $this->language->get('entry_country');
    	$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['entry_password'] = $this->language->get('entry_password');
    	$this->data['entry_confirm'] = $this->language->get('entry_confirm');

		$this->data['button_continue'] = $this->language->get('button_continue');
    	if (isset($this->error['error_warning_login'])) {
			$this->data['error_warning_login'] = $this->error['error_warning_login'];
		} else {
			$this->data['error_warning_login'] = '';
		}
		
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}	
		
		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}		
	
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}
		
		if (isset($this->error['username'])) {
			$this->data['error_username'] = $this->error['username'];
		} else {
			$this->data['error_username'] = '';
		}
		
		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}
		
		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}
		
 		if (isset($this->error['confirm'])) {
			$this->data['error_confirm'] = $this->error['confirm'];
		} else {
			$this->data['error_confirm'] = '';
		}
		
  		if (isset($this->error['address_1'])) {
			$this->data['error_address_1'] = $this->error['address_1'];
		} else {
			$this->data['error_address_1'] = '';
		}
		
		if (isset($this->error['address_2'])) {
			$this->data['error_address_2'] = $this->error['address_2'];
		} else {
			$this->data['error_address_2'] = '';
		}
   		
		if (isset($this->error['city'])) {
			$this->data['error_city'] = $this->error['city'];
		} else {
			$this->data['error_city'] = '';
		}
		
		if (isset($this->error['postcode'])) {
			$this->data['error_postcode'] = $this->error['postcode'];
		} else {
			$this->data['error_postcode'] = '';
		}

		if (isset($this->error['country'])) {
			$this->data['error_country'] = $this->error['country'];
		} else {
			$this->data['error_country'] = '';
		}

		if (isset($this->error['zone'])) {
			$this->data['error_zone'] = $this->error['zone'];
		} else {
			$this->data['error_zone'] = '';
		}
		
		if (isset($this->error['error_username_p1'])) {
			$this->data['error_username_p1'] = $this->error['error_username_p1'];
		} else {
			$this->data['error_username_p1'] = '';
		}
		
				
		if (isset($this->error['error_password_p1'])) {
			$this->data['error_password_p1'] = $this->error['error_password_p1'];
		} else {
			$this->data['error_password_p1'] = '';
		}
		
    	$this->data['action'] = $this->url->link('common/payment_online', '', 'SSL');
		$this->data['action_login'] = $this->url->link('common/payment_online/login', '', 'SSL');
		
		if (isset($this->request->post['username'])) {
    		$this->data['username'] = $this->request->post['username'];
		} else {
			$this->data['username'] = '';
		}
		
		if (isset($this->request->post['firstname'])) {
    		$this->data['firstname'] = $this->request->post['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
    		$this->data['lastname'] = $this->request->post['lastname'];
		} else {
			$this->data['lastname'] = '';
		}
		
		if (isset($this->request->post['email'])) {
    		$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = '';
		}
		
		if (isset($this->request->post['telephone'])) {
    		$this->data['telephone'] = $this->request->post['telephone'];
		} else {
			$this->data['telephone'] = '';
		}
		
		if (isset($this->request->post['fax'])) {
    		$this->data['fax'] = $this->request->post['fax'];
		} else {
			$this->data['fax'] = '';
		}
		
		if (isset($this->request->post['company'])) {
    		$this->data['company'] = $this->request->post['company'];
		} else {
			$this->data['company'] = '';
		}
		
		
		if (isset($this->request->post['address_1'])) {
    		$this->data['address_1'] = $this->request->post['address_1'];
		} else {
			$this->data['address_1'] = '';
		}

		if (isset($this->request->post['address_2'])) {
    		$this->data['address_2'] = $this->request->post['address_2'];
		} else {
			$this->data['address_2'] = '';
		}

		if (isset($this->request->post['postcode'])) {
    		$this->data['postcode'] = $this->request->post['postcode'];
		} else {
			$this->data['postcode'] = '';
		}
		
		if (isset($this->request->post['city'])) {
    		$this->data['city'] = $this->request->post['city'];
		} else {
			$this->data['city'] = '';
		}

    	if (isset($this->request->post['country_id'])) {
      		$this->data['country_id'] = $this->request->post['country_id'];
		} else {	
      		$this->data['country_id'] = $this->config->get('config_country_id');
    	}

    	if (isset($this->request->post['zone_id'])) {
      		$this->data['zone_id'] = $this->request->post['zone_id']; 	
		} else {
      		$this->data['zone_id'] = '';
    	}
		
		$this->load->model('localisation/country');
		
    	$this->data['countries'] = $this->model_localisation_country->getCountries();
		
		if (isset($this->request->post['password'])) {
    		$this->data['password'] = $this->request->post['password'];
		} else {
			$this->data['password'] = '';
		}
		
		if (isset($this->request->post['confirm'])) {
    		$this->data['confirm'] = $this->request->post['confirm'];
		} else {
			$this->data['confirm'] = '';
		}
		
		if (isset($this->request->post['newsletter'])) {
    		$this->data['newsletter'] = $this->request->post['newsletter'];
		} else {
			$this->data['newsletter'] = '';
		}	
		
		
		
		// dunglt add for shipping address
		if (isset($this->error['shipping_firstname'])) {
			$this->data['error_shipping_firstname'] = $this->error['shipping_firstname'];
		} else {
			$this->data['error_shipping_firstname'] = '';
		}	
		//print_r($this->error);
		//echo '-----------------------------------------'.$this->data['error_shipping_firstname'];
		
		if (isset($this->error['shipping_lastname'])) {
			$this->data['error_shipping_lastname'] = $this->error['shipping_lastname'];
		} else {
			$this->data['error_shipping_lastname'] = '';
		}		

		if (isset($this->request->post['shipping_company'])) {
    		$this->data['shipping_company'] = $this->request->post['shipping_company'];
		} else {
			$this->data['shipping_company'] = '';
		}
		if (isset($this->error['shipping_telephone'])) {
			$this->data['error_shipping_telephone'] = $this->error['shipping_telephone'];
		} else {
			$this->data['error_shipping_telephone'] = '';
		}
		
	
		
  		if (isset($this->error['shipping_address_1'])) {
			$this->data['error_shipping_address_1'] = $this->error['shipping_address_1'];
		} else {
			$this->data['error_shipping_address_1'] = '';
		}
		
		if (isset($this->error['shipping_address_2'])) {
			$this->data['error_shipping_address_2'] = $this->error['shipping_address_2'];
		} else {
			$this->data['error_shipping_address_2'] = '';
		}
   		
		if (isset($this->error['shipping_city'])) {
			$this->data['error_shipping_city'] = $this->error['shipping_city'];
		} else {
			$this->data['error_shipping_city'] = '';
		}
		
		if (isset($this->error['shipping_postcode'])) {
			$this->data['error_shipping_postcode'] = $this->error['shipping_postcode'];
		} else {
			$this->data['error_shipping_postcode'] = '';
		}

		if (isset($this->request->post['shipping_firstname'])) {
    		$this->data['shipping_firstname'] = $this->request->post['shipping_firstname'];
		} else {
			$this->data['shipping_firstname'] = '';
		}

		if (isset($this->request->post['shipping_lastname'])) {
    		$this->data['shipping_lastname'] = $this->request->post['shipping_lastname'];
		} else {
			$this->data['shipping_lastname'] = '';
		}
		
				
		if (isset($this->request->post['shipping_telephone'])) {
    		$this->data['shipping_telephone'] = $this->request->post['shipping_telephone'];
		} else {
			
			$this->data['shipping_telephone'] = '';
		}
		
		
		if (isset($this->request->post['shipping_address_1'])) {
    		$this->data['shipping_address_1'] = $this->request->post['shipping_address_1'];
		} else {
			$this->data['shipping_address_1'] = '';
		}

		if (isset($this->request->post['shipping_address_2'])) {
    		$this->data['shipping_address_2'] = $this->request->post['shipping_address_2'];
		} else {
			$this->data['shipping_address_2'] = '';
		}

		if (isset($this->request->post['shipping_postcode'])) {
    		$this->data['shipping_postcode'] = $this->request->post['shipping_postcode'];
		} else {
			$this->data['shipping_postcode'] = '';
		}
		
		if (isset($this->request->post['shipping_city'])) {
    		$this->data['shipping_city'] = $this->request->post['shipping_city'];
		} else {
			$this->data['shipping_city'] = '';
		}

    	if (isset($this->request->post['shipping_country_id'])) {
      		$this->data['shipping_country_id'] = $this->request->post['shipping_country_id'];
		} else {	
      		$this->data['shipping_country_id'] = $this->config->get('config_shipping_country_id');
    	}

    	if (isset($this->request->post['shipping_zone_id'])) {
      		$this->data['shipping_zone_id'] = $this->request->post['shipping_zone_id']; 	
		} else {
      		$this->data['shipping_zone_id'] = '';
    	}
		
		// end dunglt add for shipping address
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info) {
				$this->data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_account_id'), 'SSL'), $information_info['title'], $information_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}
		
		if (isset($this->request->post['agree'])) {
      		$this->data['agree'] = $this->request->post['agree'];
		} else {
			$this->data['agree'] = false;
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/payment_online.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/payment_online.tpl';
		} else {
			$this->template = 'default/template/common/payment_online.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
				
		$this->response->setOutput($this->render());	
  	}

  	private function validate() {
		/*
		if ((utf8_strlen($this->request->post['username']) < 1) || (utf8_strlen($this->request->post['username']) > 32)) {
      		$this->error['username'] = $this->language->get('error_username');
    	}
		*/
    	if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
      		$this->error['firstname'] = $this->language->get('error_firstname');
    	}

    	if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
      		$this->error['lastname'] = $this->language->get('error_lastname');
    	}

    	if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
      		$this->error['email'] = $this->language->get('error_email');
    	}

    	//if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
      	//	$this->error['warning'] = $this->language->get('error_exists');
    	//}
		
    	if ((utf8_strlen($this->request->post['telephone']) < 1) || (utf8_strlen($this->request->post['telephone']) > 32)) {
      		$this->error['telephone'] = $this->language->get('error_telephone');
    	}

    	if ((utf8_strlen($this->request->post['address_1']) < 1) || (utf8_strlen($this->request->post['address_1']) > 128)) {
      		$this->error['address_1'] = $this->language->get('error_address_1');
    	}
		if ((utf8_strlen($this->request->post['address_2']) < 1) || (utf8_strlen($this->request->post['address_2']) > 128)) {
      		$this->error['address_2'] = $this->language->get('error_address_2');
    	}

    	if ((utf8_strlen($this->request->post['city']) < 1) || (utf8_strlen($this->request->post['city']) > 128)) {
      		$this->error['city'] = $this->language->get('error_city');
    	}

		$this->load->model('localisation/country');
		
		$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
		
		if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
			$this->error['postcode'] = $this->language->get('error_postcode');
		}

    	if ($this->request->post['country_id'] == '') {
      		$this->error['country'] = $this->language->get('error_country');
    	}
		
    	if ($this->request->post['zone_id'] == '') {
      		$this->error['zone'] = $this->language->get('error_zone');
    	}
		/*
    	if ((utf8_strlen($this->request->post['password']) < 1) || (utf8_strlen($this->request->post['password']) > 30)) {
      		$this->error['password'] = $this->language->get('error_password');
    	}

    	if ($this->request->post['confirm'] != $this->request->post['password']) {
      		$this->error['confirm'] = $this->language->get('error_confirm');
    	}
		*/
		/*
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info && !isset($this->request->post['agree'])) {
      			$this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
			}
		}*/
		
    	if (!$this->error) {
			$this->data['error_info'] = false;
      		return true;
    	} else {
			$this->data['error_info'] = true;
      		return false;
    	}
  	}
  private function validateshipping() {
		
    	if ((utf8_strlen($this->request->post['shipping_firstname']) < 1) || (utf8_strlen($this->request->post['shipping_firstname']) > 32)) {
      		$this->error['shipping_firstname'] = $this->language->get('error_shipping_firstname');
    	}

    	if ((utf8_strlen($this->request->post['shipping_lastname']) < 1) || (utf8_strlen($this->request->post['shipping_lastname']) > 32)) {
      		$this->error['shipping_lastname'] = $this->language->get('error_shipping_lastname');
    	}

    	
    	if ((utf8_strlen($this->request->post['shipping_telephone']) < 1) || (utf8_strlen($this->request->post['shipping_telephone']) > 32)) {
      		$this->error['shipping_telephone'] = $this->language->get('error_shipping_telephone');
    	}

    	if ((utf8_strlen($this->request->post['shipping_address_1']) < 1) || (utf8_strlen($this->request->post['shipping_address_1']) > 128)) {
      		$this->error['shipping_address_1'] = $this->language->get('error_shipping_address_1');
    	}
		if ((utf8_strlen($this->request->post['shipping_address_2']) < 1) || (utf8_strlen($this->request->post['shipping_address_2']) > 128)) {
      		$this->error['shipping_address_2'] = $this->language->get('error_shipping_address_2');
    	}

    	if ((utf8_strlen($this->request->post['shipping_city']) < 1) || (utf8_strlen($this->request->post['shipping_city']) > 128)) {
      		$this->error['shipping_city'] = $this->language->get('error_shipping_city');
    	}

		$this->load->model('localisation/country');
		
		$country_info = $this->model_localisation_country->getCountry($this->request->post['shipping_country_id']);
		
		if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['shipping_postcode']) < 1) || (utf8_strlen($this->request->post['shipping_postcode']) > 20)) {
			$this->error['shipping_postcode'] = $this->language->get('error_shipping_postcode');
		}

    	if ($this->request->post['shipping_country_id'] == '') {
      		$this->error['country'] = $this->language->get('error_country');
    	}
		
    	if ($this->request->post['shipping_zone_id'] == '') {
      		$this->error['zone'] = $this->language->get('error_zone');
    	}
	//print_r($this->error);
    	

		/*
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info && !isset($this->request->post['agree'])) {
      			$this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
			}
		}*/
		
    	if (!$this->error) {
			$this->data['error_shipping'] = false;
      		return true;
    	} else {
			$this->data['error_shipping'] = true;
      		return false;
    	}
  	}
  	public function zone() {
		//$output = '<option value="">' . $this->language->get('text_select') . '</option>';
		
		$this->load->model('localisation/zone');

    	$results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
        
      	foreach ($results as $result) {
        	$output .= '<option value="' . $result['zone_id'] . '"';
	
	    	if (isset($this->request->get['zone_id']) && ($this->request->get['zone_id'] == $result['zone_id'])) {
	      		$output .= ' selected="selected"';
	    	}
	
	    	$output .= '>' . $result['name'] . '</option>';
    	} 
		
		if (!$results) {
		  	$output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
		}
	
		$this->response->setOutput($output);
  	}  
	public function initSesstionGuestCheckout()
	{
		$this->session->data['guest']['firstname'] = $this->request->post['firstname'];
		$this->session->data['guest']['lastname'] = $this->request->post['lastname'];
		$this->session->data['guest']['email'] = $this->request->post['email'];
		$this->session->data['guest']['telephone'] = $this->request->post['telephone'];
		$this->session->data['guest']['fax'] = $this->request->post['fax'];
		
		$this->session->data['guest']['payment']['firstname'] = $this->request->post['firstname'];
		$this->session->data['guest']['payment']['lastname'] = $this->request->post['lastname'];				
		$this->session->data['guest']['payment']['company'] = $this->request->post['company'];
		$this->session->data['guest']['payment']['address_1'] = $this->request->post['address_1'];
		$this->session->data['guest']['payment']['address_2'] = $this->request->post['address_2'];
		$this->session->data['guest']['payment']['postcode'] = $this->request->post['postcode'];
		$this->session->data['guest']['payment']['city'] = $this->request->post['city'];
		$this->session->data['guest']['payment']['country_id'] = $this->request->post['country_id'];
		$this->session->data['guest']['payment']['zone_id'] = $this->request->post['zone_id'];
		$this->load->model('localisation/country');
				
		$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
		
		if ($country_info) {
			$this->session->data['guest']['payment']['country'] = $country_info['name'];	
			$this->session->data['guest']['payment']['iso_code_2'] = $country_info['iso_code_2'];
			$this->session->data['guest']['payment']['iso_code_3'] = $country_info['iso_code_3'];
			$this->session->data['guest']['payment']['address_format'] = $country_info['address_format'];
		} else {
			$this->session->data['guest']['payment']['country'] = '';	
			$this->session->data['guest']['payment']['iso_code_2'] = '';
			$this->session->data['guest']['payment']['iso_code_3'] = '';
			$this->session->data['guest']['payment']['address_format'] = '';
		}
					
		$this->load->model('localisation/zone');

		$zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
		
		if ($zone_info) {
			$this->session->data['guest']['payment']['zone'] = $zone_info['name'];
			$this->session->data['guest']['payment']['zone_code'] = $zone_info['code'];
		} else {
			$this->session->data['guest']['payment']['zone'] = '';
			$this->session->data['guest']['payment']['zone_code'] = '';
		}
		/*
		if (isset($this->request->post['shipping_address']) && $this->request->post['shipping_address']) {
			$this->session->data['guest']['shipping_address'] = true;
		} else {
			$this->session->data['guest']['shipping_address'] = false;
		}
		*/
		if (isset($this->request->post['isshipping']) && $this->request->post['isshipping']) {
			$this->session->data['guest']['shipping_address'] = true;
		} else {
			$this->session->data['guest']['shipping_address'] = false;
		}
		
		if ($this->session->data['guest']['shipping_address']) {
			$this->session->data['guest']['shipping']['firstname'] = $this->request->post['shipping_firstname'];
			$this->session->data['guest']['shipping']['lastname'] = $this->request->post['shipping_lastname'];
			$this->session->data['guest']['shipping']['company'] = $this->request->post['shipping_company'];
			$this->session->data['guest']['shipping']['address_1'] = $this->request->post['shipping_address_1'];
			$this->session->data['guest']['shipping']['address_2'] = $this->request->post['shipping_address_2'];
			$this->session->data['guest']['shipping']['postcode'] = $this->request->post['shipping_postcode'];
			$this->session->data['guest']['shipping']['city'] = $this->request->post['shipping_city'];
			
			$this->session->data['guest']['shipping']['country_id'] = $this->request->post['shipping_country_id'];
			$this->session->data['guest']['shipping']['zone_id'] = $this->request->post['shipping_zone_id'];
			
			if ($country_info) {
				$this->session->data['guest']['shipping']['country'] = $country_info['name'];	
				$this->session->data['guest']['shipping']['iso_code_2'] = $country_info['iso_code_2'];
				$this->session->data['guest']['shipping']['iso_code_3'] = $country_info['iso_code_3'];
				$this->session->data['guest']['shipping']['address_format'] = $country_info['address_format'];
			} else {
				$this->session->data['guest']['shipping']['country'] = '';	
				$this->session->data['guest']['shipping']['iso_code_2'] = '';
				$this->session->data['guest']['shipping']['iso_code_3'] = '';
				$this->session->data['guest']['shipping']['address_format'] = '';
			}
			$zone_info = $this->model_localisation_zone->getZone($this->request->post['shipping_zone_id']);
			if ($zone_info) {
				$this->session->data['guest']['shipping']['zone'] = $zone_info['name'];
				$this->session->data['guest']['shipping']['zone_code'] = $zone_info['code'];
			} else {
				$this->session->data['guest']['shipping']['zone'] = '';
				$this->session->data['guest']['shipping']['zone_code'] = '';
			}
			
		} else {
				$this->session->data['guest']['shipping']['firstname'] = $this->request->post['firstname'];
				$this->session->data['guest']['shipping']['lastname'] = $this->request->post['lastname'];
				$this->session->data['guest']['shipping']['company'] = $this->request->post['company'];
				$this->session->data['guest']['shipping']['address_1'] = $this->request->post['address_1'];
				$this->session->data['guest']['shipping']['address_2'] = $this->request->post['address_2'];
				$this->session->data['guest']['shipping']['postcode'] = $this->request->post['postcode'];
				$this->session->data['guest']['shipping']['city'] = $this->request->post['city'];
				$this->session->data['guest']['shipping']['country_id'] = $this->request->post['country_id'];
				$this->session->data['guest']['shipping']['zone_id'] = $this->request->post['zone_id'];
				$this->session->data['guest']['shipping']['country'] = $this->session->data['guest']['payment']['country'];	
				$this->session->data['guest']['shipping']['iso_code_2'] = $this->session->data['guest']['payment']['iso_code_2'];
				$this->session->data['guest']['shipping']['iso_code_3'] = $this->session->data['guest']['payment']['iso_code_3'];
				$this->session->data['guest']['shipping']['address_format'] = $this->session->data['guest']['payment']['address_format'];
				if ($zone_info) {
				$this->session->data['guest']['shipping']['zone'] = $zone_info['name'];
				$this->session->data['guest']['shipping']['zone_code'] = $zone_info['code'];
				} else {
					$this->session->data['guest']['shipping']['zone'] = '';
					$this->session->data['guest']['shipping']['zone_code'] = '';
				}
			
		}
		
		//exit();
		unset($this->session->data['shipping_methods']);
		unset($this->session->data['shipping_method']);
		unset($this->session->data['payment_methods']);
		unset($this->session->data['payment_method']);			
	}
	
	public function initSesstionPaymentShipping()
	{
			$this->load->model('account/address');
			
					if ($this->customer->isLogged()) {					
						$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);		
					} elseif (isset($this->session->data['guest'])) {
						$shipping_address = $this->session->data['guest']['shipping'];
					}	
					// shipping methods
					
					//if (!isset($this->session->data['shipping_methods'])) {
							$quote_data = array();
							
							$this->load->model('setting/extension');
							
							$results = $this->model_setting_extension->getExtensions('shipping');
							
							foreach ($results as $result) {
								if ($this->config->get($result['code'] . '_status')) {
									$this->load->model('shipping/' . $result['code']);
									
									$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address); 
						
									if ($quote) {
										$quote_data[$result['code']] = array( 
											'title'      => $quote['title'],
											'quote'      => $quote['quote'], 
											'sort_order' => $quote['sort_order'],
											'error'      => $quote['error']
										);
									}
								}
							}
					
							$sort_order = array();
						  
							foreach ($quote_data as $key => $value) {
								$sort_order[$key] = $value['sort_order'];
							}
					
							array_multisort($sort_order, SORT_ASC, $quote_data);
							
							$this->session->data['shipping_methods'] = $quote_data;
							//print_r($this->session->data['shipping_methods']);
							/*if (isset($this->session->data['shipping_methods']['flat']['quote']['flat'])) {		
								$this->session->data['shipping_method'] = $this->session->data['shipping_methods']['flat']['quote']['flat'];
							} 
							*/
							if (isset($this->session->data['shipping_methods']['free']['quote']['free'])) {		
								$this->session->data['shipping_method'] = $this->session->data['shipping_methods']['free']['quote']['free'];
							} 
						//}
					
					// end shipping methods
			
			$total_data = array();					
			$total = 0;
			$taxes = $this->cart->getTaxes();
			
			$this->load->model('setting/extension');
			
			$sort_order = array(); 
			
			$results = $this->model_setting_extension->getExtensions('total');
			
			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
			}
			
			array_multisort($sort_order, SORT_ASC, $results);
			
			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					$this->load->model('total/' . $result['code']);
		
					$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
				}
			}
			// Payment Methods
			$method_data = array();
			
			$this->load->model('setting/extension');
			if ($this->customer->isLogged()) {
				$payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);		
			} elseif (isset($this->session->data['guest'])) {
				$payment_address = $this->session->data['guest']['payment'];
			}	
			$results = $this->model_setting_extension->getExtensions('payment');
			
			
			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					$this->load->model('payment/' . $result['code']);
					
					$method = $this->{'model_payment_' . $result['code']}->getMethod($payment_address, $total); 
					
					if ($method) {
						$method_data[$result['code']] = $method;
					}
				}
			}
			$this->session->data['payment_methods'] = $method_data;
			//$this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
			//print_r($this->session->data['payment_methods']);
			if(isset($this->session->data['payment_methods']) && isset($this->session->data['payment_methods']['free_checkout'])) {
				$this->session->data['payment_method'] = $this->session->data['payment_methods']['free_checkout'];
			} 
			//print_r($this->session->data['payment_methods']);
			//end dunglt
	}
}
?>